//
//  NSURL+parse.m
//  storeapp
//
//  Created by huangyi on 13-1-10.
//  Copyright (c) 2013年 ios. All rights reserved.
//

#import "NSURL+parse.h"

@implementation NSURL (parse)

-(NSURL*)setParameterString:(NSString*)string{
    NSMutableString *url=[NSMutableString string];
    [url appendString:self.scheme];
    [url appendString:@"://"];
    if (self.user!=nil) {
        [url appendString:self.user];
    }
    if (self.password!=nil) {
        [url appendString:@":"];
        [url appendString:self.password];
    }
    if (self.user!=nil||self.password!=nil) {
        [url appendString:@"@"];
    }
    [url appendString:self.host];
    [url appendString:@":"];
    [url appendString:self.port.stringValue];
    [url appendString:self.path];
    if (string!=nil) {
        [url appendString:@";"];
        [url appendString:string];
    }
    if (self.query!=nil) {
        [url appendString:@"?"];
        [url appendString:self.query];
    }
    if (self.fragment!=nil) {
        [url appendString:@"#"];
        [url appendString:self.fragment];
    }
    return [NSURL URLWithString:url];
}

-(NSURL*)setQuery:(NSString*)query{
    NSMutableString *url=[NSMutableString string];
    [url appendString:self.scheme];
    [url appendString:@"://"];
    if (self.user!=nil) {
        [url appendString:self.user];
    }
    if (self.password!=nil) {
        [url appendString:@":"];
        [url appendString:self.password];
    }
    if (self.user!=nil||self.password!=nil) {
        [url appendString:@"@"];
    }
    [url appendString:self.host];
    [url appendString:@":"];
    [url appendString:self.port.stringValue];
    [url appendString:self.path];
    if (self.parameterString!=nil) {
        [url appendString:@";"];
        [url appendString:self.parameterString];
    }
    if (query!=nil) {
        [url appendString:@"?"];
        [url appendString:query];
    }
    if (self.fragment!=nil) {
        [url appendString:@"#"];
        [url appendString:self.fragment];
    }
    return [NSURL URLWithString:url];
}
-(NSURL*)setFragment:(NSString*)fragment{
    NSMutableString *url=[NSMutableString string];
    [url appendString:self.scheme];
    [url appendString:@"://"];
    if (self.user!=nil) {
        [url appendString:self.user];
    }
    if (self.password!=nil) {
        [url appendString:@":"];
        [url appendString:self.password];
    }
    if (self.user!=nil||self.password!=nil) {
        [url appendString:@"@"];
    }
    [url appendString:self.host];
    [url appendString:@":"];
    [url appendString:self.port.stringValue];
    [url appendString:self.path];
    if (self.parameterString!=nil) {
        [url appendString:@";"];
        [url appendString:self.parameterString];
    }
    if (self.query!=nil) {
        [url appendString:@"?"];
        [url appendString:self.query];
    }
    if (fragment!=nil) {
        [url appendString:@"#"];
        [url appendString:fragment];
    }
    return [NSURL URLWithString:url];
}

@end
